﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace devoirBD2
{
    public partial class FrmListeCommandes : Form
    {
        private CommandeTable commande_table;
        
        public FrmListeCommandes()
        {
            commande_table = CommandeTable.getInstance();
            InitializeComponent();
        }

        private void FrmListeCommandes_Load(object sender, EventArgs e)
        {
            remplirListe();
        }

        public void remplirListe()
        {
            lstCommandes.Items.Clear();

            commande_table.loadCommandes();

            foreach (Commande cmd in commande_table.getCommandes())
            {
                lstCommandes.Items.Add(new ListViewItem(cmd.getLigne()));
            }

        }

        private void btnAjouter_Click(object sender, EventArgs e)
        {
            FrmCommande frm = new FrmCommande(this);

            frm.ShowDialog();
        }

        private void btnModifier_Click(object sender, EventArgs e)
        {
            FrmCommande frm = new FrmCommande(this, commande_table.getCommandes()[lstCommandes.SelectedIndices[0]]);

            frm.ShowDialog();
        }

        private void lstCommandes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnModifier.Enabled = (lstCommandes.SelectedItems.Count == 1);
            btnSupprimer.Enabled = (lstCommandes.SelectedItems.Count > 0);
        }

        private void btnSupprimer_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Êtes-vous certain de vouloir supprimer ces commandes ?", "Suppression", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int[] indices = new int[lstCommandes.SelectedIndices.Count];
                lstCommandes.SelectedIndices.CopyTo(indices, 0);

                commande_table.deleteCommandes(indices);

                remplirListe();
            }
        }
    }
}
